package com.neusoft.elmboot.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.elmboot.Dto.RoleReqDto;
import com.neusoft.elmboot.Vo.PermVo;
import com.neusoft.elmboot.common.constant.RoleAndPermisserionConstant;
import com.neusoft.elmboot.mapper.RolePermissionMapper;
import com.neusoft.elmboot.po.Role;
import com.neusoft.elmboot.mapper.RoleMapper;
import com.neusoft.elmboot.po.RolePermission;
import com.neusoft.elmboot.service.RoleService;
import org.springframework.beans.BeanUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色表(Role)表服务实现类
 *
 * @author makejava
 * @since 2024-03-23 15:14:31
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Role queryById(Integer id) {
        return this.roleMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param role 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Role> queryByPage(Role role, PageRequest pageRequest) {
        long total = this.roleMapper.count(role);
        return new PageImpl<>(this.roleMapper.queryAllByLimit(role, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param roleReqDto 实例对象
     * @return 实例对象
     */
    @Override
    public Role insert(RoleReqDto roleReqDto) {
        Role r = new Role();
        BeanUtils.copyProperties(roleReqDto,r);
        this.roleMapper.insert(r);
        return r;
    }

    /**
     * 修改数据
     *
     * @param roleDto 实例对象
     * @return 实例对象
     */
    @Override
    public Role update(RoleReqDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);
        this.roleMapper.update(role);
        return this.queryById(role.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.roleMapper.deleteById(id) > 0;
    }

    @Override
    public List<Role> query1(RoleReqDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);
        return this.roleMapper.queryAllByLimit(role, null);
    }

    @Override
    public List<Integer> queryPermsIdByRoleId(Integer roleId, List<PermVo> permVos) {
        ArrayList<Integer> list = new ArrayList<>();
        if (RoleAndPermisserionConstant.SUPER_ADMIN_ROLE_ID == roleId){
            permVos.forEach(p->{
                list.add(Integer.parseInt(p.getId()));
            });
        }
        List<RolePermission> rolePermissions = rolePermissionMapper.selectByRoleId(roleId.longValue());

        rolePermissions.forEach(m->list.add(m.getPermissionId()));
        return list;


    }

    @Override
    public Boolean savePermission(Integer roleId, List<Integer> permissions) {
        rolePermissionMapper.deleteByRoleId(roleId.longValue());
        Map<String,Object> params = new HashMap<>();
        params.put("roleId",roleId);
        params.put("permIds",permissions);
        int count = rolePermissionMapper.insertBatch(params);
        return count> 0 ? true :false;


    }
}
